﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.JHWorkflowTrans;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.BuzEnum;
using Microhard.Core;

namespace Microhard.WarehouseInsp.Buz.JHWorkflow
{
    public class OutInPutTransDJImpl : ITransDataInDJ
    {
        public Core.DBUtility.ExecutedResult SaveToDJ(string mainID, Guid pkid, string currentUserLoginCode)
        {
            ExecutedResult result = new ExecutedResult();
            WarehouseInspEntities wdb = new WarehouseInspEntities();
            C6Entities db = new C6Entities();
            //保存申请表
            var outinput = wdb.OutInPutApplies.SingleOrDefault(w => w.OutInPutApplyID == pkid);
            if (outinput == null)
            {
                result.ErrorMsg.Add("OutInPutTransDJImpl.SaveToDJ", "无法获取业务实体数据，PKID:" + pkid);
                return result;
            }
            MH_OutInPutApply add = new MH_OutInPutApply();
            add.MainID = mainID;
            add.CreateTime = DateTime.Now;
            //获得最新警戒信息
            var alt = outinput.SupProject.SupervisionDetails.Where(c => c.Status.Value == (int)PrjStatus.Finished).OrderByDescending(c => c.CreateTime);
            if (alt.Count() > 0)
            {
                var first = alt.First();
                add.GuardLine = first.Guardline.ToString();
                add.AlertLine = first.WarnAmount.ToString();
            }
            //获取当前项目库存总额
            add.CurrentPlgAmount = wdb.PledgeChangeRecords.Where(p => p.PledgePlanInRecord.ProjectID == outinput.SupProject.ProjectID
                && p.IsUsing == true).Sum(p => p.CurrentAmount).ToString();
            add.PrjEnd = outinput.SupProject.PrjEndTime.Value.ToString("yyyy-MM-dd");
            add.PrjID = outinput.SupProject.ProjectID;
            add.PrjName = outinput.SupProject.Name;
            add.PrjStart = outinput.SupProject.PrjStartTime.Value.ToString("yyyy-MM-dd");
            db.MH_OutInPutApply.AddObject(add);

            //保存出入库明细
            foreach (var d in outinput.OutInPutWarehouseRecords)
            {
                MH_OutInPutDetail detail = new MH_OutInPutDetail
                {
                    MainID = mainID,
                    OutInAmount = d.OutInAmount.Value.ToString(),
                    OutInCount = d.OutInCount.Value.ToString(),
                    OutInPrice = d.OutInPrice.Value.ToString(),
                    PlgName = d.PledgePlanInRecord.ProductList.Name,
                    PlnPdgID = d.PlnPdgID,
                    WarehouseID = d.PledgePlanInRecord.WarehouseID,
                    WarehouseName = d.PledgePlanInRecord.Warehouse.Name
                };
                db.MH_OutInPutDetail.AddObject(detail);
            }

            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error("OutInPutTransDJImpl.SaveToDJ", ex);
            }
            return result;
        }

        public void UpdateBuzDataStatus(Guid buzmainID, List<ApproveInfo> approveInfo, bool approvedStatus)
        {
            if (approveInfo == null)
            {
                Log.Error("OutInPutTransDJImpl.UpdateBuzDataStatus", "ProjectID:" + buzmainID + "无法获取审批信息");
                return;
            }
            //获取业务数据
            WarehouseInspEntities db = new WarehouseInspEntities();
            var entity = db.OutInPutApplies.SingleOrDefault(w => w.OutInPutApplyID == buzmainID);
            if (entity == null)
            {
                Log.Error("OutInPutTransDJImpl.UpdateBuzDataStatus", "OutInPutApplyID:" + buzmainID + "获取实体失败");
                return;
            }
            //更新审批信息
            entity.ApproveDate = DateTime.Now;
            if (approvedStatus)
            {
                entity.State = (int)PrjStatus.Finished;
            }
            else
            {
                entity.State = (int)PrjStatus.RollBack;
            }
            //更新出入库明细审批状态
            var sups = entity.OutInPutWarehouseRecords.Where(w => w.Status == (int)PrjStatus.Approving);
            foreach (var s in sups)
            {
                s.Status = entity.State;
            }
            //保存审批意见
            foreach (var a in approveInfo)
            {
                ApproveRecord ar = new ApproveRecord
                {
                    ApproveEndTime = a.ApproveEndTime,
                    ApproverName = a.ApproverName,
                    CreateTime = DateTime.Now,
                    Info = a.Info,
                    IsPass = a.IsPass,
                    PKID = buzmainID
                };
                db.ApproveRecords.AddObject(ar);
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error("OutInPutTransDJImpl.UpdateBuzDataStatus", ex);
            }
        }

        public string WorkflowCode
        {
            get { return "OutInPutApply"; }
        }
    }
}
